/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.cq.bd.utils;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * md5加密、比较工具
 * 
 * @author huangmy
 * @date   2009-10-2
 */
public final class MD5Util {
    /**
     * 加密
     * 
     * @param msg
     * @return
     */
    public static String encrypt(String msg) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");            
            byte[] rs = md.digest(msg.getBytes("UTF-8"));
            if (rs != null && rs.length > 0) {
                StringBuffer sb = new StringBuffer();
                for (byte b : rs) {
                    sb.append(StringUtil.toHexString(b, false));
                }
                return sb.toString();
            }
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return null;
    }
    
    /**
     * 比较两个是否相等
     * 
     * @param t
     * @param o
     * @return
     */
    public static boolean eq(String t, String o) {
        return encrypt(t).equals(encrypt(o));
    }
}
